@echo off
echo ===================================================
echo       PandaFactor Startup Script
echo ===================================================
echo.

:: Set color
color 0A

echo [1/4] Checking environment...
:: Check if Conda is in PATH
where conda >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
    echo Error: Conda command not found. Please make sure Anaconda or Miniconda is installed and added to PATH
    goto error
)

echo [2/4] Starting PandaFactor database...
:: Start database
cd /d "%~dp0"
start cmd /k "title PandaFactor Database && echo Starting database... && cd mongodb 8.0.8\bin && db_start.bat"
echo Database startup command executed, please wait for database to fully start...
timeout /t 5 /nobreak > nul

echo [3/4] Activating virtual environment and starting Web service...
:: Activate virtual environment and start Web service
start cmd /k "title PandaFactor Web Service && echo Activating virtual environment and starting Web service... && call conda activate pandaai && cd /d "%~dp0panda_factor\panda_web" && python panda_web\main.py"
echo Web service startup command executed, please wait for service to start...
timeout /t 3 /nobreak > nul

echo [4/4] Starting backend API service...
:: Start backend API service
start cmd /k "title PandaFactor API Service && echo Starting backend API service... && call conda activate pandaai && cd /d "%~dp0panda_factor\panda_server" && python panda_server\__main__.py"
echo Backend API service startup command executed...

echo.
echo ===================================================
echo       PandaFactor Services Started
echo ===================================================
echo  - Database: MongoDB is running
echo  - Web Service: http://localhost:8080/factor/
echo  - API Service: http://localhost:8111/
echo.
echo  Please do not close the command windows, closing them will stop the services
echo  To stop all services, close all opened command windows or use stop_panda_factor.bat
echo ===================================================
echo.

goto end

:error
color 0C
echo.
echo An error occurred during startup, please check the error message above
echo.
pause
exit /b 1

:end
echo Press any key to exit this window (services will continue running in background)...
pause > nul
exit /b 0 